package com.nuanshui.heatedloan.controller.display.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.facade.CustomerCompanyInfoService;
import com.nuanshui.heatedloan.credit.form.CustomerCompanyInfoForm;
import com.nuanshui.heatedloan.credit.vo.CustomerCompanyInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wangxw on 2017/7/4.
 */

@RequestMapping("credit/company")
@RestController
public class CustomerCompanyInfoController extends BaseController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerCompanyInfoService customerCompanyInfoService;


    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String saveOrUpdate(String customerCompanyInfoStr){
        if(StringUtils.isEmpty(customerCompanyInfoStr)){
            return error("请传入参数");
        }
        String userId= getCurrentUser().getId();
        CustomerCompanyInfoForm customerCompanyInfoForm= JSON.parseObject(customerCompanyInfoStr,CustomerCompanyInfoForm.class);
        customerCompanyInfoForm.setStatus(1);
        customerCompanyInfoForm.setUserId(userId);
        if(!SysStringUtils.isEmpty(customerCompanyInfoForm.getId())){
            customerCompanyInfoService.update(customerCompanyInfoForm);
        }else{
            customerCompanyInfoService.save(customerCompanyInfoForm);
        }
        return success(customerCompanyInfoForm);
    }
    @RequestMapping(value = "/getInfoById",method = RequestMethod.POST)
    public String getInfoById(String id){
        CustomerCompanyInfoVO customerCompanyInfoVO=new CustomerCompanyInfoVO();
        customerCompanyInfoVO=customerCompanyInfoService.loadVO(id);
        return success(customerCompanyInfoVO);
    }
    @RequestMapping(value = "/getInfoByUserId",method = RequestMethod.POST)
    public String getInfoByUserId(){
        String userId= getCurrentUser().getId();
        CustomerCompanyInfoVO customerCompanyInfoVO=new CustomerCompanyInfoVO();
        customerCompanyInfoVO=  customerCompanyInfoService.loadVOByUserId(userId);
        return success(customerCompanyInfoVO);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String delete(String id){
        customerCompanyInfoService.delete(id);
        return success();
    }

}
